<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <el-card>
        <!-- headeer -->
        <el-row style="height: 60px" type="flex" justify="end">
          <el-col>
            <el-button
              size="small"
              type="primary"
              @click="addPage(6666)"
            >添加页面</el-button>
          </el-col>
          <el-col>
            <div>
              <el-input
                v-model="pageSearch.title_contains"
                placeholder="请输入关键字"
              >
                <el-button slot="append" @click="selectFn">筛选</el-button>
              </el-input>
            </div>
          </el-col>
        </el-row>
        <hr>
        <!-- 表格 -->
        <el-table :data="pageList" border style="width: 100%">
          <el-table-column prop="title" label="标题" width="180" />
          <el-table-column prop="content" label="内容">
            <template #default="{ row }">
              <div v-html="row.content" />
            </template>
          </el-table-column>
          <el-table-column width="180" label="操作">
            <template #default="{ row }">
              <el-row type="flex" justify="center">
                <el-button
                  size="small"
                  type="primary"
                  @click="addPage(row.id)"
                >编辑</el-button>
                <el-button
                  size="small"
                  type="danger"
                  @click="delPage(row.id)"
                >删除</el-button>
              </el-row>
            </template>
          </el-table-column>
          <hr>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[1, 2, 3, 4, 5, 6]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-size="pageSearch._limit"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹框 -->
      <add-page
        :is-show-dialog.sync="isShowAddPage"
        :page-id="pageId"
        @update="updateFn"
      />
    </div>
  </div>

</template>
<script>
import addPage from '@/views/pageTable/components/add-page.vue'
import { pageListApi, delPageApi, pageCountApi } from '@/api/page'
export default {
  components: {
    addPage
  },
  data() {
    return {
      pageSearch: {
        _limit: 2,
        _start: 0,
        title_contains: ''
      },
      currentPage: 1,
      total: 0,
      pageList: [],
      isShowAddPage: false,
      pageId: 6666
    }
  },
  created() {
    this.getPageList()
    this.getPageListCount()
  },
  methods: {
    // 点击分页条数
    handleSizeChange(pageSize) {
      this.pageSearch._limit = pageSize
      this.getPageList()
      this.getPageListCount()
    },
    //  点击页码刷新
    handleCurrentChange(currentIndex) {
      this.pageSearch._start = (currentIndex - 1) * this.pageSearch._limit
      this.currentPage = currentIndex
      console.log(this.pageSearch._start)
      this.getPageList()
      this.getPageListCount()
    },
    // 刷新
    updateFn() {
      this.getPageList()
      this.getPageListCount()
    },
    // 总页数
    async getPageListCount(params) {
      const res = await pageCountApi(params)
      this.total = res.data
      console.log(129, '总条数', this.total)
    },
    // 增加
    addPage(id) {
      this.isShowAddPage = true
      this.pageId = id
    },
    // 获取页面列表
    async getPageList() {
      const { data } = await pageListApi(this.pageSearch)
      this.pageList = data
      console.log(133, data, this.total)
    },
    // 删除
    async delPage(id) {
      // 二次确认
      await this.$confirm('确定删除吗？')
      // 发送删除请求
      await delPageApi(id)
      // 提示用户
      this.$message.success('删除成功')
      if (this.pageList.length === 1 && this.total > 1) {
        this.currentPage--
        this.pageSearch._start -= this.pageSearch._limit
      }
      // 重新获取数据
      await this.getPageList()
      await this.getPageListCount({
        _limit: this.pageSearch._limit,
        _start: this.pageSearch._start
      })
    },
    // 筛选页面
    async selectFn() {
      console.log(8888)
      const { data } = await pageListApi(
        {
          _limit: 2,
          _start: 0,
          title_contains: this.pageSearch.title_contains
        })
      this.getPageListCount({
        _limit: 2,
        _start: 0,
        title_contains: this.pageSearch.title_contains
      })
      this.pageList = data
    }
  }
}

</script>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

</style>
